/*
 * Created on Nov 13, 2003
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.vectrics.common.reporting;


import java.util.*;
import java.sql.Connection;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import org.vectrics.VectricsException;
import org.vectrics.common.reporting.pdf.*;
import org.vectrics.struts.Request;

import java.io.OutputStream;


public abstract class ReportManager {
	private transient static Logger log = Logger.getLogger(ReportManager.class);
	private String databaseJndi = null;
	private String formPath = null;		
	private Map parameters = new HashMap();
	private HttpServletRequest request = null;
	
	public ReportManager(String database) {
		super();
		this.databaseJndi = database;		
		log.debug("Database JNDI name = " + database);
	}
	
	protected abstract String getCompiledReportsPath();
	//protected abstract void setReportParameters(Map parameters);
	
	protected final String getFormPath() {
		return(this.formPath);
	}
	protected Map getReportParameters() {
		return(parameters);		
	}
	
	protected HttpServletRequest getRequest() {
		return(request);
	}
	
	abstract public BaseReport createReport(Request request, OutputStream outStream) throws VectricsException;
	
	
	private Connection getConnection(DataSource dataSource) {
		Connection connection = null;
		try {
			log.debug("getConnection() - Getting db connection...");
			connection = dataSource.getConnection();
			log.debug("getConnection() - acquired connection");
		} catch (Exception se) {
			log.error("getConnection()", se);
		}
        
		log.debug("getConnection() - finished");
		return(connection);
   }

	
	private DataSource getDataSource() {
		log.debug("getDataSources() called");
		DataSource dataSource = null;
		try {
			InitialContext ic = new InitialContext();
			Object obj = ic.lookup(this.databaseJndi);
			log.info("DataSource JNDI class = " + obj.getClass().getName());
			dataSource = (DataSource)obj;
			if (dataSource == null) {
				log.error("getDataSources() - could not find JNDI entry for: " + this.databaseJndi);
			}
		} catch (javax.naming.NamingException ne) {
			log.error("getDataSources() - Exception getting datasource: " + this.databaseJndi, ne);
		}
		return(dataSource);
	}
	
	/*  FUTURE -  JASPERREPORTS NEEDED
	public byte[] compileAndMakeReportBytes(String fileName) {
		log.info("compileAndMakeReportBytes()");
		byte[] reportBytes = null;
		try {
			//DataSource dataSource = new DataSource();
			Class.forName("com.mysql.jdbc.Driver");
			String sqlUrl = "jdbc:mysql://localhost/vectrics";
			Connection connection = DriverManager.getConnection(sqlUrl, "mmoore", "rabbit");
			
			//ServletContext context = this.getServletConfig().getServletContext();
			InitialContext context = new InitialContext();
			//File reportFile = new File("CandidateReport.xml");
			//String fileName = "CandidateReport.xml";

			Map parameters = new HashMap();
			parameters.put("ReportTitle", "Address Report");
			parameters.put("BaseDir", ".");
			JasperPrint jasperPrint = null;
	
			try
			{
				log.info("Compiling report...");
				JasperReport jasperReport = JasperCompileManager.compileReport(fileName); 
				
				
				URL url = new URL("http://192.168.1.3/recruit/custom/report-templates/CandidateReport.xml");
				InputStream inputStream = url.openStream();	
				
				log.info("Printing report...");
				jasperPrint = 
					JasperFillManager.fillReport(
						jasperReport,
						parameters, connection);
						
				log.info("Exporting to PDF report...");
				//JasperExportManager.exportReportToPdfFile(jasperPrint, "report.pdf");
				reportBytes = JasperExportManager.exportReportToPdf(jasperPrint);
				//reportStream = new ByteArrayInputStream(bytes);
				
				
			}
			catch (JRException e)
			{
				log.error("Error running report", e);
			}
		}
		catch (Exception ex) {
			log.error("Error setting up report", ex);
			
		}
		return(reportBytes);
	}
	*/	
}
